package com.kingdee.signup;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.iflytek.speech.RecognizerResult;
import com.iflytek.speech.SpeechError;
import com.iflytek.ui.RecognizerDialog;
import com.iflytek.ui.RecognizerDialogListener;
import com.kingdee.signup.net.DataResult;
import com.kingdee.signup.utils.BitmapUtils;
import com.kingdee.signup.utils.CommonUtils;

public class FeedBackActivity extends BaseActivity
  implements View.OnClickListener, RecognizerDialogListener
{
  private static final String IMG_NAME = "feedback.png";
  private static final String IMG_PARENT;
  private static final int RESULT_CAPTURE_IMAGE = 1;
  private static final int RESULT_PHOTO_IMAGE = 2;
  private static final String TAKE_IMG_NAME = "feedback.jpg";
  private static final String TAKE_IMG_PARENT = Environment.getExternalStorageDirectory().toString().trim() + "/DCIM/Camera/";
  private final int MAX_LENGTH = 140;
  private int Rest_Length = 140;
  private ProgressDialog dialog;
  private RecognizerDialog iatDialog;
  private AlertDialog mAlertDialog;
  private ImageView mBack;
  private Bitmap mBitmap;
  private Button mButton;
  private Context mContext;
  private TextView mCountTxt;
  private EditText mEdit;
  private String mImgPath = TAKE_IMG_PARENT + "feedback.jpg";
  private TextView mMsgTxt;
  private ImageView mPhoto;
  private SharedPreferences mSharedPreferences;
  private ImageView mSiri;
  private TextView mTitle;
  private TextView mWeiBoTxt;

  static
  {
    IMG_PARENT = Environment.getExternalStorageDirectory().toString().trim() + "/signup/";
  }

  private Bitmap getImage(String paramString)
  { 
     Bitmap localBitmap=null; 
    try
    {
      if (new File(paramString).exists())
      {
    	DecimalFormat localDecimalFormat = new DecimalFormat("0");
        android.graphics.BitmapFactory.Options localOptions = new BitmapFactory.Options();
        localOptions.inPreferredConfig = Bitmap.Config.ALPHA_8;
        localOptions.inJustDecodeBounds = true;
        localOptions.inSampleSize = 1; 
        BitmapFactory.decodeFile(paramString, localOptions);
        Log.e("debug", "原图宽:" + localOptions.outWidth);
        Log.e("debug", "原图高:" + localOptions.outHeight);
        if (localOptions.outWidth < localOptions.outHeight){
        	 if (localOptions.outHeight > 100)
             {
               double d1 = localOptions.outHeight / 50.0F;
               if ((d1 > 1.0D) && (d1 < 2.0D));
               for (localOptions.inSampleSize = 2; ; localOptions.inSampleSize = Integer.valueOf(localDecimalFormat.format(d1)).intValue())
               {
                 Log.e("debug", "比:" + d1);
                 break;
               }
             }
        }
        if (localOptions.outWidth <= 100)
        {
        	 localOptions.inSampleSize = 1;
        }
        
        double d2 = localOptions.outWidth / 50.0F;
        if ((d2 > 1.0D) && (d2 < 2.0D));
        for (localOptions.inSampleSize = 2; ; localOptions.inSampleSize = Integer.valueOf(localDecimalFormat.format(d2)).intValue())
        {
          Log.e("debug", "比:" + d2);
          localOptions.inPreferredConfig = Bitmap.Config.RGB_565;
          localOptions.inJustDecodeBounds = false;
          localBitmap = BitmapFactory.decodeFile(paramString, localOptions);
          Log.e("debug", "压缩后宽:" + localBitmap.getWidth());
          Log.e("debug", "压缩后高:" + localBitmap.getHeight());
          break;
        }
      }
    }
    catch (Exception localException)
    { 
    }
    return localBitmap;
  }

  private void initView()
  {
    this.mBack = ((ImageView)findViewById(2131493105));
    this.mBack.setOnClickListener(this);
    this.mBack.setImageResource(2130837553);
    this.mTitle = ((TextView)findViewById(2131493106));
    this.mTitle.setText("意见反馈");
    this.mButton = ((Button)findViewById(2131493107));
    this.mButton.setOnClickListener(this);
    this.mButton.setText("发送");
    this.mSiri = ((ImageView)findViewById(2131493081));
    this.mSiri.setOnClickListener(this);
    this.mWeiBoTxt = ((TextView)findViewById(2131493084));
    this.mWeiBoTxt.setVisibility(8);
    this.mMsgTxt = ((TextView)findViewById(2131493086));
    this.mMsgTxt.setVisibility(8);
    this.mPhoto = ((ImageView)findViewById(2131493082));
    this.mPhoto.setOnClickListener(this);
    this.mCountTxt = ((TextView)findViewById(2131493087));
    findViewById(2131493083).setVisibility(8);
    findViewById(2131493085).setVisibility(8);
    this.mEdit = ((EditText)findViewById(2131493080));
//    this.mEdit.addTextChangedListener(new TextWatcher()
//    {
//      public void afterTextChanged(Editable paramEditable)
//      {
//        FeedBackActivity.this.mCountTxt.setText(FeedBackActivity.this.Rest_Length);
//      }
//
//      public void beforeTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3)
//      {
//        FeedBackActivity.this.mCountTxt.setText(FeedBackActivity.this.Rest_Length);
//      }
//
//      public void onTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3)
//      {
//        if (FeedBackActivity.this.Rest_Length > 0)
//          FeedBackActivity.this.Rest_Length = (140 - FeedBackActivity.this.mEdit.getText().length());
//      }
//    });
    this.iatDialog = new RecognizerDialog(this, "appid=" + getString(2131296259));
    this.iatDialog.setListener(this);
    this.mSharedPreferences = getSharedPreferences(getPackageName(), 0);
  }

  private void operatorBMP()
  {
    this.mBitmap = getImage(this.mImgPath);
    this.mPhoto.setImageBitmap(this.mBitmap);
    BitmapUtils.saveBmpToPng(this.mBitmap, IMG_PARENT, "feedback.png");
  }

  private void showPhoto()
  {
    CharSequence[] arrayOfCharSequence = new CharSequence[4];
    arrayOfCharSequence[0] = "相册";
    arrayOfCharSequence[1] = "拍照";
    arrayOfCharSequence[2] = "默认图片";
    arrayOfCharSequence[3] = "取消";
    if (this.mAlertDialog == null)
    {
      this.mAlertDialog = new AlertDialog.Builder(this).setTitle("选择图片").setItems(arrayOfCharSequence, new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface paramDialogInterface, int paramInt)
        {
          Intent localIntent1=null;
          File localFile2=null;
          if (paramInt == 1)
          {
            localIntent1 = new Intent("android.media.action.IMAGE_CAPTURE");
            File localFile1 = new File(FeedBackActivity.TAKE_IMG_PARENT);
            if (!localFile1.exists())
              localFile1.mkdir();
            localFile2 = new File(FeedBackActivity.TAKE_IMG_PARENT, "feedback.jpg");
            if (localFile2.exists());
          }
          while (true)
          {
            try
            {
              localFile2.createNewFile();
              localIntent1.putExtra("output", Uri.fromFile(localFile2));
              FeedBackActivity.this.startActivityForResult(localIntent1, 1);
              FeedBackActivity.this.mAlertDialog = null;
              localFile2.delete();

              if (paramInt == 0)
              {
                Intent localIntent2 = new Intent("android.intent.action.GET_CONTENT");
                localIntent2.setType("image/*");
                FeedBackActivity.this.startActivityForResult(localIntent2, 2);
                continue;
              }
              if (paramInt == 2)
              {
                File localFile3 = new File(FeedBackActivity.IMG_PARENT, "feedback.png");
                if (localFile3.exists())
                  localFile3.delete();
                FeedBackActivity.this.mPhoto.setImageResource(2130837654);
                continue;
              }
              if (paramInt != 3)
                continue;
              paramDialogInterface.dismiss();
              
              return;
            }
            catch (IOException localIOException2)
            {
              localIOException2.printStackTrace();
              continue;
            }
          }
        }
      }).create();
      this.mAlertDialog.setOnCancelListener(new DialogInterface.OnCancelListener()
      {
        public void onCancel(DialogInterface paramDialogInterface)
        {
          FeedBackActivity.this.mAlertDialog = null;
        }
      });
      this.mAlertDialog.show();
    }
  }

  public DataResult doFetchData(DataResult paramDataResult)
    throws Exception
  {
      return paramDataResult; 
//    SignUpAPI localSignUpAPI = new SignUpAPI(this, true);
//    switch (paramDataResult.getType())
//    {
//    default:
//    case 1113:
//    }
//    while (true)
//    {
//      String str1 = "#金蝶移动签到#" + this.mEdit.getText().toString() + "（Android" + Build.VERSION.RELEASE + ",V" + CommonUtils.getVerName(this.mContext) + "）@熊明菊";
//      if (new File(IMG_PARENT + "feedback.png").exists());
//      for (String str2 = IMG_PARENT + "feedback.png"; ; str2 = "")
//      {
//        BaseResult localBaseResult = localSignUpAPI.sendMsg(str1, str2);
//        if (localBaseResult == null)
//        {paramDataResult.setStatu(1);}
//        paramDataResult.setResult(localBaseResult);
//        paramDataResult.setStatu(0);
//        break;
//      }
//      return paramDataResult; 
//    }
  }

  public void doProcessData(DataResult paramDataResult)
  {
//    switch (paramDataResult.getType())
//    {
//    case 1113:
//    	switch (paramDataResult.getStatu())
//        {
//        case 0:
//          BaseResult localBaseResult = (BaseResult)paramDataResult.getResult();
//          if (localBaseResult != null)
//          {
//            if (localBaseResult.getStatus() != 1)
//            {}
//            Toast.makeText(this, "发送成功", 1).show();
//            finish();
//          }
//          if (localBaseResult.getStatus() == -1)
//          {
//            Toast.makeText(this, "系统出现未知异常", 1).show();
//            continue;
//          }
//          Toast.makeText(this, "数据获取失败", 1).show();
//        case 1:
//           this.dialog.dismiss();
//        default:
//            break; 
//        }
//    }
  }

  protected void onActivityResult(int paramInt1, int paramInt2, Intent paramIntent)
  {
//    switch (paramInt1)
//    {
//    default:
//    case 1:
//    case 2:
//    }
//    while (true)
//    {
//      super.onActivityResult(paramInt1, paramInt2, paramIntent);
//      return;
//      if (paramInt2 == -1)
//      {
//        if (paramIntent != null)
//        {
//          Uri localUri2 = paramIntent.getData();
//          if (localUri2 != null);
//          while (true)
//          {
//            try
//            {
//              if ("".equals(localUri2))
//                break label141;
//              Cursor localCursor2 = getContentResolver().query(localUri2, null, null, null, null);
//              if (localCursor2 == null)
//                break label121;
//              localCursor2.moveToFirst();
//              this.mImgPath = localCursor2.getString(1);
//              operatorBMP();
//            }
//            catch (Exception localException)
//            {
//              localException.printStackTrace();
//            }
//            break;
//            label121: this.mImgPath = localUri2.toString().split("//")[1];
//            continue;
//            label141: this.mImgPath = (TAKE_IMG_PARENT + "feedback.jpg");
//          }
//        }
//        this.mImgPath = (TAKE_IMG_PARENT + "feedback.jpg");
//        operatorBMP();
//        continue;
//      }
//      new File(this.mImgPath).deleteOnExit();
//      continue;
//      if (paramIntent == null)
//        continue;
//      Uri localUri1 = paramIntent.getData();
//      String[] arrayOfString = new String[1];
//      arrayOfString[0] = "_data";
//      Cursor localCursor1 = managedQuery(localUri1, arrayOfString, null, null, null);
//      int i = localCursor1.getColumnIndexOrThrow("_data");
//      localCursor1.moveToFirst();
//      this.mImgPath = localCursor1.getString(i);
//      Log.e("debug", "路径:" + this.mImgPath);
//      operatorBMP();
//    }
  }

  public void onClick(View paramView)
  {
    switch (paramView.getId())
    {
		    case 2131493105:
		        finish(); 
		    case 2131493107:
		        if (!this.mEdit.getText().toString().equals(""))
		        {
		    	  CommonUtils.addConfigInfo(mContext, "MyAdd",this.mEdit.getText().toString());
//		          HashMap localHashMap = new HashMap();
//		          localHashMap.put("更多功能", "意见反馈提交");
//		          Constants.onEvent(this.mContext, "eventid_more", localHashMap);
//		          this.dialog = AndroidUtils.showDialog(this, "数据加载中，请稍等...");
//		          doConnection(1113); 
		        }else{
		        	Toast.makeText(this, "请输入发送内容", 1).show();
		        }
		    case 2131493081:
		    	showIatDialog();
		    case 2131493082:
		    	showPhoto();
		    default:
		    	return;
    }

  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(2130903084);
    this.mContext = this;
    initView();
  }

  public void onEnd(SpeechError paramSpeechError)
  {
  }

  public void onIntoScreenEnd()
  {
  }

  public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent)
  {
    return false;
  }

  public void onLeaveScreen()
  {
  }

  public void onLeaveScreenEnd()
  {
  }

  public void onResults(ArrayList<RecognizerResult> paramArrayList, boolean paramBoolean)
  {
//    StringBuilder localStringBuilder = new StringBuilder();
//    Iterator localIterator = paramArrayList.iterator();
//    while (true)
//    {
//      if (!localIterator.hasNext())
//      {
//        StringBuffer localStringBuffer = new StringBuffer();
//        localStringBuffer.append(localStringBuilder);
//        int i = this.mEdit.getSelectionStart();
//        this.mEdit.getText().insert(i, localStringBuffer.toString());
//        return;
//      }
//      localStringBuilder.append(((RecognizerResult)localIterator.next()).text.replaceAll("[。&*（）……%￥#@！《，；‘“：》<>.,/?？]", ""));
//    }
  }

  public void showIatDialog()
  {
//    String str1 = this.mSharedPreferences.getString(getString(2131296298), getString(2131296303));
//    String str2 = null;
//    if ("poi".equals(str1))
//    {
//      String str4 = getString(2131296307);
//      String str5 = this.mSharedPreferences.getString(getString(2131296305), str4);
//      String str6 = getString(2131296311);
//      String str7 = this.mSharedPreferences.getString(getString(2131296309), str6);
//      if (!str4.equals(str5))
//      {
//        str2 = "area=" + str5;
//        if (!str6.equals(str7))
//          str2 = str2 + str7;
//      }
//    }
//    this.iatDialog.setEngine(str1, str2, null);
//    String str3 = this.mSharedPreferences.getString(getString(2131296313), getString(2131296315));
//    if (str3.equals("rate8k"))
//      this.iatDialog.setSampleRate(SpeechConfig.RATE.rate8k);
//    while (true)
//    {
//      this.iatDialog.show();
//      return;
//      if (str3.equals("rate11k"))
//      {
//        this.iatDialog.setSampleRate(SpeechConfig.RATE.rate11k);
//        continue;
//      }
//      if (str3.equals("rate16k"))
//      {
//        this.iatDialog.setSampleRate(SpeechConfig.RATE.rate16k);
//        continue;
//      }
//      if (!str3.equals("rate22k"))
//        continue;
//      this.iatDialog.setSampleRate(SpeechConfig.RATE.rate22k);
//    }
  }
}